package com.qfedu.controller;

import com.alibaba.fastjson.JSONObject;
import com.qfedu.entity.Consumer;
import com.qfedu.entity.SongList;
import com.qfedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author zhang
 * @Title: commonValidateAdd
 * @date : 2022/2/25 9:10
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping("/user")
    @ResponseBody
    public List<Consumer> findAllConsumer(){
        return userService.findAllConsumer();
    }


   @RequestMapping("/user/detail")
   @ResponseBody
   public List<Consumer> findAllConsumerById(int id){
        return userService.findAllConsumerById(id);
   }


    @RequestMapping("/user/delete")
    @ResponseBody
    public int deleteConsumerById(Consumer consumer) {
        return userService.deleteConsumerById(consumer);
    }

    @RequestMapping("/user/add")
    @ResponseBody
    public JSONObject addConsumer(Consumer consumer, @RequestParam(value = "phone_num") String phoneNum){

        Date date = new Date();
        consumer.setCreateTime(date);
        consumer.setUpdateTime(date);
        consumer.setPhoneNum(phoneNum);

        System.out.println(consumer);

        JSONObject jsonObject = new JSONObject();
        int i = userService.addConsumer(consumer);
        if (i>0){
            jsonObject.put("code",1);
            jsonObject.put("msg","注册成功");

        }else {
            jsonObject.put("code",0);
            jsonObject.put("msg","注册失败");
        }
        return jsonObject;
    }

    @RequestMapping("/user/update")
    @ResponseBody
    public JSONObject updateConsumer(Consumer consumer){

        Date date = new Date();
        consumer.setCreateTime(date);
        consumer.setUpdateTime(date);

        int i = userService.updateConsumer(consumer);

        JSONObject jsonObject = new JSONObject();
        if (i > 0){
            jsonObject.put("code",1);
            jsonObject.put("msg","修改成功");
        }else {
            jsonObject.put("code",0);
            jsonObject.put("msg","修改失败");
        }
        return jsonObject;
    }

    @RequestMapping("/user/avatar/update")
    @ResponseBody
    public JSONObject updateConsumerById(MultipartFile file, Consumer consumer) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String url = "/img/avatorImages/" + originalFilename;//originalFilename 文件名
        originalFilename = "D:\\qianfeng\\software\\IDEA\\project3-background\\project3background\\img\\avatorImages\\" + originalFilename;
        System.out.println("originalFilename");

        //文件上传
        File file1 = new File(originalFilename);
        //将接收到的文件转移到新的文件下
        file.transferTo(file1);


        consumer.setAvator(url);
        JSONObject jsonObject = userService.updateConsumerById(consumer);
        jsonObject.put("pic",url);
        return jsonObject;

    }


}
